{% load humanize %}
<script>
	$(function() {

		
		var name = $( "#id_name" ),
			type = $( "#id_type" ),
			balance = $( "#id_balance" ),
      description = $( "#id_description" ),
			allFields = $( [] ).add( name ).add( type ).add( balance ).add( description ),
			tips = $( ".validateTips" );

		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}

		function checkLength( o, n, min, max ) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( "Length of " + n + " must be between " +
					min + " and " + max + "." );
				return false;
			} else {
				return true;
			}
		}

		function checkRegexp( o, regexp, n ) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
			} else {
				return true;
			}
		}
		
		$( "#dialog-form" ).dialog({
			autoOpen: false,
			height: 600,
			width: 476,
			modal: true,
			buttons: {
				"Create an account": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					bValid = bValid && checkLength( name, "name", 3, 80 );

					bValid = bValid && checkRegexp( name, /^[a-zA-Z]([0-9a-zA-Z_])+$/i, "Name may consist of a-z, A-Z, 0-9, underscores, begin with a letter." );
					bValid = bValid && checkRegexp( balance, /^[0-9\.\-]+$/i, "Balance must be number. e.g. 5000" );

					if ( bValid ) {
						$( "#create-account-form" ).submit();
						
					}
				},
				Cancel: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});

		$( "#create-account" )
			.button()
			.click(function() {
				$( "#dialog-form" ).dialog( "open" );
			});
	});
	</script>
  <style type="text/css">
ul.module-accounts {
  padding:0px 0px 0px 5px;
  width: 190px;
}
ul.module-accounts li{
  display: block;
  list-style-image: none;
  list-style-position: outside;
  list-style-type: none;
}

ul.module-accounts li.account div.module-accounts-name {
  margin-bottom: 0px;
  margin-left: 0px;
  margin-right: 0px;
  margin-top: 0px;
  
}
ul.module-accounts li.account div.module-accounts-name span.balance{
  float: right;
}

label, input { display:block; }
input.text { margin-bottom:12px; width:95%; padding: .4em; }
fieldset { padding:0; border:0; margin-top:25px; }
.ui-dialog .ui-state-error { padding: .3em; }
.validateTips { border: 1px solid transparent; padding: 0.3em; }
	</style>
Account List
	<ul class="module-accounts">
    {% for account in accounts %}
    <li class="account">
      <div class="module-accounts-name">
        <span class="balance">{{ account.balance|intcomma }}</span>
        <a class="accountName " href="{% url transaction-by-account account.id %}">{{ account.name }}</a>
      </div>
      <div class="module-accounts-detail">
        {{ account.description }}
      </div>
    </li>    
    {% endfor %}
    <li class="account summary">
      <div class="module-accounts-name">
        <span class="balance">{{ summary_balance|intcomma }}</span>
        <a class="accountName" >Net Worth</a>
      </div>
    </li>
	</ul>
  <a href="#" id="create-account">New account</a>
  
  <div id="dialog-form" title="Create new account">
	<p class="validateTips">All form fields are required.</p>
  <form method="POST" action="{% url account-add %}" id="create-account-form">
  {% csrf_token %}
	<fieldset>
		<label for="id_name">Name:</label>
		<input id="id_name" type="text" name="name" class="text ui-widget-content ui-corner-all" maxlength="250" />
    <label for="id_type">Type:</label>
    <input id="id_type" type="text" name="type" class="text ui-widget-content ui-corner-all" maxlength="250" />
		<label for="id_balance">Balance:</label>
    <input id="id_balance" type="text" name="balance"value="" class="text ui-widget-content ui-corner-all" maxlength="42" />
		<label for="id_description">Description:</label>
    <textarea id="id_description" rows="5" cols="40" value="" class="text ui-widget-content ui-corner-all" name="description"></textarea>
	</fieldset>
	</form>
</div>